/*     */ package com.zimbra.cs.iochannel;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.JSONElement;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.cs.session.Session;
/*     */ import com.zimbra.cs.session.SessionCache;
/*     */ import com.zimbra.cs.session.SoapSession;
/*     */ import com.zimbra.cs.session.SoapSession.RemoteNotifications;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.nio.ByteBuffer;
/*     */ import java.util.Collection;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentException;
/*     */ import org.dom4j.DocumentHelper;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CrossServerNotification
/*     */   extends Message
/*     */ {
/*     */   public static final String AppId = "xsn";
/*     */   private String accountId;
/*     */   private String sessionId;
/*     */   private String payload;
/*     */   
/*     */   public static CrossServerNotification create(SoapSession session, ZimbraSoapContext zsc)
/*     */     throws MessageChannelException
/*     */   {
/*     */     Element soapElement;
/*     */     try
/*     */     {
/*  45 */       soapElement = Element.create(SoapProtocol.Soap12, "base");
/*     */     } catch (ServiceException e) {
/*  47 */       throw MessageChannelException.CannotCreate("element");
/*     */     }
/*  49 */     session.putNotifications(soapElement, zsc, 0);
/*  50 */     Element notifyElement = soapElement.getOptionalElement("notify");
/*  51 */     if (notifyElement == null) {
/*  52 */       throw MessageChannelException.CannotCreate("no notification");
/*     */     }
/*  54 */     notifyElement.addAttribute("seq", (String)null);
/*  55 */     StringBuilder soap = new StringBuilder();
/*     */     try {
/*  57 */       notifyElement.marshal(soap);
/*     */     } catch (IOException e) {
/*  59 */       throw MessageChannelException.CannotCreate("marshall");
/*     */     }
/*  61 */     String accountId = zsc.getAuthtokenAccountId();
/*  62 */     return new CrossServerNotification(accountId, "", soap.toString());
/*     */   }
/*     */   
/*     */ 
/*     */   protected int size()
/*     */   {
/*  68 */     return 2 * (this.accountId.length() + this.sessionId.length() + this.payload.length()) + 12;
/*     */   }
/*     */   
/*     */   protected void serialize(ByteBuffer buffer)
/*     */     throws IOException
/*     */   {
/*  74 */     writeString(buffer, this.accountId);
/*  75 */     writeString(buffer, this.sessionId);
/*  76 */     writeString(buffer, this.payload);
/*     */   }
/*     */   
/*     */   protected Message construct(ByteBuffer buffer) throws IOException
/*     */   {
/*  81 */     return new CrossServerNotification(buffer);
/*     */   }
/*     */   
/*     */   public String getAppId()
/*     */   {
/*  86 */     return "xsn";
/*     */   }
/*     */   
/*     */   public String getRecipientAccountId()
/*     */   {
/*  91 */     return this.accountId;
/*     */   }
/*     */   
/*     */   public String getSessionId() {
/*  95 */     return this.sessionId;
/*     */   }
/*     */   
/*     */   public String getPayload() {
/*  99 */     return this.payload;
/*     */   }
/*     */   
/*     */   CrossServerNotification() {}
/*     */   
/*     */   public CrossServerNotification(ByteBuffer buffer)
/*     */     throws IOException
/*     */   {
/* 107 */     this.accountId = readString(buffer);
/* 108 */     this.sessionId = readString(buffer);
/* 109 */     this.payload = readString(buffer);
/*     */   }
/*     */   
/*     */   private CrossServerNotification(String aid, String sid, String ntfn)
/*     */   {
/* 114 */     this.accountId = aid;
/* 115 */     this.sessionId = sid;
/* 116 */     this.payload = ntfn;
/*     */   }
/*     */   
/*     */   public Message.MessageHandler getHandler()
/*     */   {
/* 121 */     new Message.MessageHandler()
/*     */     {
/*     */       public void handle(Message m, String clientId) {
/* 124 */         if (!(m instanceof CrossServerNotification)) {
/* 125 */           return;
/*     */         }
/* 127 */         CrossServerNotification message = (CrossServerNotification)m;
/* 128 */         Collection<Session> sessions = SessionCache.getSoapSessions(m.getRecipientAccountId());
/* 129 */         if (sessions == null) {
/* 130 */           Message.log.warn("no active sessions for account %s", new Object[] { m.getRecipientAccountId() });
/* 131 */           return;
/*     */         }
/* 133 */         SoapSession.RemoteNotifications soapNtfn = null;SoapSession.RemoteNotifications jsonNtfn = null;
/*     */         try {
/* 135 */           Document dom = DocumentHelper.parseText(message.getPayload());
/* 136 */           soapNtfn = new SoapSession.RemoteNotifications(Element.convertDOM(dom.getRootElement(), Element.XMLElement.mFactory));
/* 137 */           jsonNtfn = new SoapSession.RemoteNotifications(Element.convertDOM(dom.getRootElement(), Element.JSONElement.mFactory));
/*     */         } catch (DocumentException e) {
/* 139 */           Message.log.warn("cannot parse notification from %s", clientId, e);
/* 140 */           return;
/*     */         }
/* 142 */         for (Session session : sessions) {
/* 143 */           Message.log.debug("notifying session %s", new Object[] { session.toString() });
/* 144 */           SoapSession ss = (SoapSession)session;
/* 145 */           SoapProtocol responseProtocol = ss.getResponseProtocol();
/* 146 */           if ((responseProtocol == SoapProtocol.Soap11) || (responseProtocol == SoapProtocol.Soap12)) {
/* 147 */             ss.addRemoteNotifications(soapNtfn);
/* 148 */           } else if (responseProtocol == SoapProtocol.SoapJS) {
/* 149 */             ss.addRemoteNotifications(jsonNtfn);
/*     */           }
/* 151 */           ss.forcePush();
/*     */         }
/*     */       }
/*     */     };
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 159 */     StringBuilder buf = new StringBuilder();
/* 160 */     buf.append("xsn").append(":");
/* 161 */     buf.append(this.accountId).append(":");
/* 162 */     buf.append(this.sessionId).append(":");
/* 163 */     buf.append(this.payload);
/* 164 */     return buf.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/iochannel/CrossServerNotification.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */